package com.odm.communitymanage.ui.idle

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseFragment
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.IdleData
import com.odm.communitymanage.ui.ImageDetailActivity
import com.odm.communitymanage.util.GlideUtils
import com.odm.communitymanage.widget.AvatarModeUtils
import kotlinx.android.synthetic.main.fragment_idle_detail.*

/**
 * @description: 闲置物品详情页面
 * @author: ODM
 * @date: 2020/4/12
 */
class IdleDetailFragment:BaseFragment() {

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        registerLiveEvent()
    }


    override fun initViews() {
/*        ivPhoto_1_IdleDetail.setOnClickListener {
            LogUtils.d("LiveEvent post $imgUrl")
            LiveEventBus
                .get(Constants.IMAGE_URL)
                .post(imgUrl)
            ActivityUtils.startActivity(ImageDetailActivity::class.java)
        }*/
    }

    /**
     * 收到来自闲置列表页面的加载详情的事件
     */
    private fun registerLiveEvent(){
        LiveEventBus.get(Constants.EVENT_IDLE_DETAIL , IdleData::class.java)
            .observeSticky(this  , object : Observer<IdleData> {
                override fun onChanged(t: IdleData?) {
                    loadIdleDetail(t)
                }
            })
    }

    /**
     * 加载 闲置详情，控件赋值
     * @param idleDetail IdleData?
     */
    private fun loadIdleDetail(idleDetail : IdleData ?){
        tvContactInfo_IdleDetail.text = idleDetail?.userName
        tvDescription_IdleDetail.text = idleDetail?.description
        tvTime_IdleDetail.text = idleDetail?.time
        tvTopic_IdleDetail.text = idleDetail?.topic
        tvPrice_IdleDetail.text = idleDetail?.price.toString()
        tvRemark_IdleDetail.text = idleDetail?.remarks
        val imgUrls = idleDetail?.imgs?: mutableListOf()
        //图片加载
        var targetView : ImageView  ?= null
        for(url in imgUrls){
            if (ivPhoto_1_IdleDetail.visibility != View.VISIBLE) {
                targetView = ivPhoto_1_IdleDetail
            } else if (ivPhoto_2_IdleDetail.visibility != View.VISIBLE) {
                targetView = ivPhoto_2_IdleDetail

            } else if (ivPhoto_3_IdleDetail.visibility != View.VISIBLE) {
                targetView = ivPhoto_3_IdleDetail
            }
            //若target仍有需要加载的目标，则取url加载
            targetView?.apply {
                visibility = View.VISIBLE
                //加载图片
                GlideUtils
                    .getInstance()
                    .loadImageUrl(requireContext(),url ,targetView)
                //点击事件，跳转加载大图Activity
                setOnClickListener {
                    LiveEventBus
                        .get(Constants.IMAGE_URL)
                        .post(url)
                    ActivityUtils.startActivity(ImageDetailActivity::class.java)
                }
            }
        }
/*        if(ivPhoto_1_IdleDetail.visibility != View.VISIBLE){
            //无图片情况下，保底在第一张图片的位置显示一张 默认图
            GlideUtils.getInstance()
                .loadImageResourceId(requireContext(),
                    R.mipmap.waiting ,
                    ivPhoto_1_IdleDetail)
        }*/
        
        setNameAvatar(idleDetail?.userName ?: "无"  , ivAvatar_IdleDetail)
        
    }


    override fun getLayoutId(): Int {
        return R.layout.fragment_idle_detail
    }



}